package com.mj.app.health.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mj.app.health.R;
import com.mj.app.health.base.BaseActivity;
import com.mj.app.health.dialog.CommonNoticeDialog;
import com.mj.app.health.dialog.ICommonDialog;
import com.mj.app.health.net.HttpGo;
import com.mj.app.health.net.HttpListener;
import com.mj.app.health.net.response.RecordResponse;
import com.mj.app.health.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * ========================================
 * *
 * title: TimeRecordActivity
 * description: 运动计时
 * *
 * Created by David Hogan on 2018/3/15.
 * ========================================
 */

public class TimeRecordActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.com_back)
    ImageView comBack;
    @BindView(R.id.com_title)
    TextView comTitle;
    @BindView(R.id.r_title)
    EditText rTitle;
    @BindView(R.id.r_description)
    EditText rDescription;
    @BindView(R.id.r_count)
    Chronometer rCount;
    @BindView(R.id.r_go)
    TextView rGo;

    private boolean isGoing = false;
    private Handler mHandler;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_time_record;
    }

    @Override
    protected void init() {

        comTitle.setText("运动计时");

        setListener();

        mHandler = new Handler() {
            @Override
            public void handleMessage(final Message msg) {
                if(msg.what == 1){

                    ToastUtil.show("上传成功");

                    // 计时器清空
                    rCount.stop();
                    rCount.setText(FormatMiss(0));

                    // 重置判断
                    isGoing = false;
                    rGo.setSelected(false);
                    rGo.setText("GO");
                    rCount.setVisibility(View.INVISIBLE);
                    rTitle.setText("");
                    rDescription.setText("");
                }else{
                    ToastUtil.show("上传失败, 请重新尝试");
                }
            }
        };
    }

    private void setListener() {

        comBack.setOnClickListener(this);
        rGo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.com_back:
                back();
                break;

            case R.id.r_go:

                if(!isGoing){ // 从头开始

                    if(TextUtils.isEmpty(rTitle.getText()) || "".equals(rTitle.getText().toString().trim())){
                        ToastUtil.show("请输入标题");
                        return;
                    }
                    if(TextUtils.isEmpty(rDescription.getText()) || "".equals(rDescription.getText().toString().trim())){
                        ToastUtil.show("请输入描述");
                        return;
                    }

                    rGo.setText("STOP");
                    isGoing = true;
                    rGo.setSelected(true);
                    rCount.setVisibility(View.VISIBLE);

                    rCount.setBase(SystemClock.elapsedRealtime());//计时器清零
                    rCount.setText(FormatMiss(0));
                    rCount.start();
                }else{ // 结束运动

                    CommonNoticeDialog noticeDialog = new CommonNoticeDialog(this);
                    noticeDialog.setMsgTxt("确定要结束运动计时吗？");
                    noticeDialog.setICommonDialog(new ICommonDialog() {
                        @Override
                        public void onSurePressed() {

                            // 保存
                            save();

                        }

                        @Override
                        public void onCancelPressed() {

                        }
                    });
                    noticeDialog.show();
                }

                break;
        }
    }

    private void save() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                final Message msg = new Message();

                HttpGo.insertRecord(rTitle.getText().toString(),
                        rDescription.getText().toString(),
                        rCount.getText().toString(),
                        new HttpListener<RecordResponse>() {
                            @Override
                            public void onSuccess(RecordResponse recordResponse) {

                                if(recordResponse.getCode() == 1){

                                    msg.what = 1;
                                    mHandler.sendMessage(msg);
                                }else{

                                    msg.what = 0;
                                    mHandler.sendMessage(msg);
                                }
                            }

                            @Override
                            public void onError(String exception) {
                                msg.what = 0;
                                mHandler.sendMessage(msg);
                            }
                        });
            }
        }).start();
    }


    private String FormatMiss(int miss){
        String hh=miss/3600>9?miss/3600+"":"0"+miss/3600;
        String mm=(miss % 3600)/60>9?(miss % 3600)/60+"":"0"+(miss % 3600)/60;
        String ss=(miss % 3600) % 60>9?(miss % 3600) % 60+"":"0"+(miss % 3600) % 60;
        return hh+"时"+mm+"分"+ss+"秒";
    }
}
